#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Unit Test for spider.Spider
"""

import unittest
from wabao.spider import Spider

class SpiderTest(unittest.TestCase):
    """Spider UnitTest Case"""
    
    start_url_old = 'http://auction1.paipai.com/7EE6792400000000007E377503D06037' # old style
    start_url_new = 'http://auction1.paipai.com/DC18690B00000000005C36BD03150798' # new
    templates_new = [
        {
        'info': ur"""
            (?:[\s\S]*?
            qqUin:"(?P<seller_id>[\s\S]*?)",
            )?
            
            (?:[\s\S]*?
            userCredit:"(?P<credit>[\s\S]*?)",
            )?
            
            (?:[\s\S]*?
            commodityPrice:"(?P<price>[\s\S]*?)",
            )?
            
            (?:[\s\S]*?
            commodityTitle:"(?P<name>[\s\S]*?)",
            )?
            
            (?:[\s\S]*?
            <script[^<>]*?>
            [\s\S]*?
            commInfo='(?P<info>[\s\S]*?)';
            [\s\S]*?
            </script>
            )?
            
            (?:[\s\S]*?
            <dd>卖家昵称：(?P<seller_name>[\s\S]*?)</dd>
            )?
            
            (?:[\s\S]*?
            <div><a[^<>]*?>
            (?P<category>[\s\S]*?)商品详情</div> 
            )?
            
            (?:[\s\S]*?
            <span>销售记录\((?P<sale_count>[^\)]*?)\)\s*</span>
            )?
            
            (?:[\s\S]*?
            <div[^<>]*?class="productinfosummary"[^<>]*?>[\s\S]*?
            <ul>(?P<summary>[\s\S]*?)</ul>
            )?
            
            (?:[\s\S]*?
            <span[^<>]*?>保[^<>]*?修：[^<>]*?</span>
            (?P<maintenance>[\s\S]*?)</li>
            )?
            
            (?:[\s\S]*?
            <span[^<>]*?>发[^<>]*?票：[^<>]*?</span>
            (?P<invoice>[\s\S]*?)</li>
            )?
            
            
#            ShippingCost
#            StartTime
#            EndTime
#            lastModified
#            (?P<attributes>[\s\S]*?)
#            
#            seller-info
            """,
        }
    ]
    
    def setUp(self):
        "Hook method for setting up the test fixture before exercising it."
        self.spider_new = Spider(self.start_url_new, self.templates_new)
    def test_spider_new(self):
        for level, url, parenturl, response, \
                    content, downurls, info in self.spider_new.walk():
            print '-' * 60
            if info:
                print level, url, parenturl, len(downurls)
                print info
                for k, v in info.iteritems():
                    print k, ':', v
            else:
                print level, url, parenturl, downurls

if __name__ == '__main__':
    unittest.main()